var prefix = ctx + "monitor/logininfo";
var $,layer;
layui.use(['table', 'form', 'element', 'layer', 'jquery'], function () {
    var element = layui.element;
    var form = layui.form;
    var table = layui.table;
    $ = layui.$;
    layer = parent.layer === undefined ? layui.layer : top.layer;

    $(function () {
        var options = {
            url: prefix + "/list",
            cleanUrl: prefix + "/clean",
            removeUrl: prefix + "/remove",
            exportUrl: prefix + "/export",
        };
        //初始化角色表
        initLoginInfoTable();
    });


    /**
     * 初始化表格
     */
    function initLoginInfoTable() {
        table.render({
            id: 'loginInfoList',
            elem: '#loginInfoList',
            method: 'POST',
            url: prefix + "/list",
            toolbar: '#toolbarLoginInfo',
            title: '登录信息',
            cols: [[
                {type: 'checkbox', fixed: 'left'},
                {type: 'numbers', fixed: 'left', title: '序号'},
                {field: 'infoId', title: '访问编号', hide: true},
                {field: 'loginName', title: '登录名称'},
                {field: 'ipaddr', title: '登录地址'},
                {field: 'loginLocation', title: '登录地点'},
                {field: 'browser', title: '浏览器'},
                {field: 'os', title: '操作系统'},
                {
                    field: 'status', title: '登录状态', align: 'center', templet: function (row) {
                        return statusTools(row);
                    }
                },
                {field: 'msg', title: '操作信息'},
                {field: 'loginTime', title: '登录时间'},
                {fixed: 'right', title: '操作', toolbar: '#barLoginInfo'}
            ]],
            page: true,
            request: {
                pageName: 'pageNum' //页码的参数名称，默认：page
                ,limitName: 'pageSize' //每页数据量的参数名，默认：limit
            },
            parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": '加载中。。。', //解析提示文本
                    "count": res.total, //解析数据长度
                    "data": res.rows     //解析数据列表
                };
            }
        });
    }

    /**
     * 监听表头工具栏事件
     */
    table.on('toolbar(loginInfoList)', function (obj) {
        var checkStatus = table.checkStatus(obj.config.id);
        switch (obj.event) {
            case 'cleanBtn':
                layer.msg(checkStatus.isAll ? '全选' : '未全选');
                break;
            case 'exportBtn':
                layer.open({
                    type: 1,
                    title:'导出',
                    content: '导出Excel' //这里content是一个普通的String
                });
                break;
        }
    });

    /**
     * 监听行工具事件
     */
    table.on('tool(loginInfoList)', function (obj) {
        var data = obj.data;
        if (obj.event === 'del') {
            layer.confirm('真的删除行么', function (index) {
                obj.del();
                layer.close(index);
            });
        }
    });

    /* 角色状态显示 */
    function statusTools(row) {
        if (row.status == 1) {
            return '<span> <i class=\"layui-icon\">&#x1007;</i>停用</span>';
        } else {
            return '<span> <i class=\"layui-icon\">&#x1005;</i>启用</span>';
        }
    }

});
